package com.pda.xinma.ui.backdepot;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.pda.xinma.R;
import com.pda.xinma.bean.BackDepotListBean;
import com.pda.xinma.bean.BackInfoBean;
import com.pda.xinma.bean.ScanBean;
import com.pda.xinma.bean.ScanResultBean;
import com.pda.xinma.bean.request.QRcodeRequest;
import com.pda.xinma.contract.BackDepotContract;
import com.pda.xinma.net.NetUtils;
import com.pda.xinma.presenter.BackDepotPresenter;
import com.pda.xinma.view.ScanButView;
import com.wangshen.base.app.AppActivityKey;
import com.wangshen.base.config.StringKey;
import com.wangshen.base.net.bean.BaseAppEntity;
import com.wangshen.base.ui.mvp.base.ui.BaseMvpActivity;
import com.wangshen.base.util.ToastUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

@Route(path = AppActivityKey.BACKDEPOTDETAILACTIVITY)
public class BackDepotDetailActivity extends BaseMvpActivity<BackDepotPresenter> implements BackDepotContract.View {
    @BindView(R.id.iv_close)
    ImageView ivClose;
    @BindView(R.id.tv_back)
    TextView tvBack;
    @BindView(R.id.tv_container)
    TextView tvContainer;
    @BindView(R.id.tv_productModel)
    TextView tvProductModel;
    @BindView(R.id.ll7)
    LinearLayout ll7;
    @BindView(R.id.tv_batch)
    TextView tvBatch;
    @BindView(R.id.ll8)
    LinearLayout ll8;
    @BindView(R.id.tv_indexNo)
    TextView tvIndexNo;
    @BindView(R.id.tv_weight)
    TextView tvWeight;
    @BindView(R.id.tv_apparentDensity)
    TextView tvApparentDensity;
    @BindView(R.id.tv_liquidity)
    TextView tvLiquidity;
    @BindView(R.id.tv_produceDate)
    TextView tvProduceDate;
    @BindView(R.id.tv_shelfLifeDate)
    TextView tvShelfLifeDate;
    @BindView(R.id.sb_scan)
    ScanButView sbScan;
    @BindView(R.id.tv_move_out)
    TextView tvMoveOut;
    @BindView(R.id.tv_storageBin)
    TextView tvStorageBin;
    @BindView(R.id.tv_measureWeight)
    TextView tvMeasureWeight;

    @Override
    public BackDepotPresenter getPresenter() {
        return new BackDepotPresenter();
    }

    @Override
    public int getView() {
        return R.layout.activity_back_depot_detail;
    }

    @Override
    public void initView() {
        Bundle bundle=getIntent().getExtras();
        String code=bundle.getString(StringKey.NO);
        presenter.queryBackInfoByNo(code);
    }

    @Override
    public void initLinsenterner() {
        sbScan.setScanCallBack(new ScanButView.ScanCallBack() {
            @Override
            public void callBack(ScanBean scanBean) {
                QRcodeRequest request = new QRcodeRequest();
                request.setQRCode(scanBean.getCode());
                request.setQRCodeType("1");
                getCode(request);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        sbScan.setScanCallBack(null);
    }

    @Override
    public void getData() {

    }

    public void getCode(QRcodeRequest request) {
        NetUtils.geApi().scanTheQRCodeToVerify(request)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<BaseAppEntity<ScanResultBean>>() {
                    @Override
                    public void accept(BaseAppEntity<ScanResultBean> stringBaseAppEntity) throws Exception {
                        if (!stringBaseAppEntity.getCode().equals("200")) {
                            ToastUtils.showToast(mContext, stringBaseAppEntity.getMessage());
                            return;
                        }
                        if (stringBaseAppEntity.getData().getType().equals("A")) {
                            tvContainer.setText(stringBaseAppEntity.getData().getCode());
                        } else if (stringBaseAppEntity.getData().getType().equals("B")) {
                            tvStorageBin.setText(stringBaseAppEntity.getData().getCode());
                        } else {
                            ToastUtils.showToast(mContext, "请扫正确的条形码");
                        }
                        if (!TextUtils.isEmpty(tvStorageBin.getText().toString()) && !TextUtils.isEmpty(tvContainer.getText().toString())){
                            presenter.queryBackInfo(stringBaseAppEntity.getData().getCode(),tvStorageBin.getText().toString());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        ToastUtils.showToast(mContext, throwable.getMessage());
                    }
                });
    }

    @Override
    public void setData(BackInfoBean backInfoBean) {
        tvContainer.setText(backInfoBean.getContainer());
        tvApparentDensity.setText(backInfoBean.getApparentDensity());
        tvBatch.setText(backInfoBean.getBatch());
        tvIndexNo.setText(backInfoBean.getIndexNo() + "");
        tvShelfLifeDate.setText(backInfoBean.getShelfLifeDate());
        tvLiquidity.setText(backInfoBean.getLiquidity());
        tvProduceDate.setText(backInfoBean.getProduceDate());
        tvProductModel.setText(backInfoBean.getProductModel());
        tvMeasureWeight.setText(backInfoBean.getActualWeight());
        tvWeight.setText(backInfoBean.getWeight());
    }

    @Override
    public void setList(List<BackDepotListBean> list) {

    }

    @Override
    public void closeSuccess() {

    }


    @OnClick({R.id.iv_close, R.id.tv_back, R.id.sb_scan, R.id.tv_move_out})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_close:
                finishActivity();
                break;
            case R.id.tv_back:
                finishActivity();
                break;
            case R.id.tv_move_out:
                String weight = "0";
                if (!TextUtils.isEmpty(tvWeight.getText().toString())) {
                    weight = tvWeight.getText().toString();
                }
                if (TextUtils.isEmpty(tvContainer.getText().toString())){
                    showMessage("桶码不能为空！");
                    return;
                }
                if (TextUtils.isEmpty(tvStorageBin.getText().toString())){
                    showMessage("作业库位不能为空！");
                    return;
                }
                presenter.addBackEntry(tvContainer.getText().toString(), weight,tvStorageBin.getText().toString(),tvMeasureWeight.getText().toString());
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
